package com.shy.springframework.beans.factory.support;

import com.shy.springframework.beans.BeansException;
import com.shy.springframework.beans.factory.BeanFactory;
import com.shy.springframework.beans.factory.config.BeanDefinition;
import com.shy.springframework.beans.factory.config.DefaultSingletonBeanRegistry;

/**
 * @author yzpang
 * @desc: 抽象BeanFactory, 实现了bean的获取与注册
 * @date: 2023/2/7 14:28
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) {
        // 1.判断是否已有单例实例
        Object bean = getSingleton(name);
        if (bean != null){
            return bean;
        }
        // 2.构建bean
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
